/**
 * 工程名: 	Quickdisk
 * 文件名: 	ListFile.java
 * 创建人:  	guo.tianxiang
 * 创建时间: 	2014-3-2 上午10:59:31
 * 版权所有：	Copyright (c) 2014 苏州科大恒星信息技术有限公司  
 * 文件描述: 描述该文件的作用
 * -----------------------------变更记录 ----------------------------- 
 * 日期        		变更人      		版本号  		变更描述  
 * ------------------------------------------------------------------  
 * 2014-3-2     guo.tianxiang   	1.0       	first created  
 */
package com.tianxiang.action;

import java.text.ParseException;
import java.util.ArrayList;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.model.MyFile;
import com.tianxiang.model.User;
import com.tianxiang.service.ListFileService;
import com.tianxiang.utils.Tools;

/**
 * 作为请求列出文件列表的Action
 * 
 * @since Quickdisk
 * @version 1.0 2014-3-2
 * @author guo.tianxiang
 */
public class ListFileAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String folder; // 接收网页传递来的请求参数，表示请求的目录
	private String parent;// 接收网页传来的请求参数，表示父级目录

	private ArrayList<MyFile> listfiles;
	private int totalFileNum;
	ListFileService listFileService;

	public int getTotalFileNum() {
		return totalFileNum;
	}

	public void setTotalFileNum(int totalFileNum) {
		this.totalFileNum = totalFileNum;
	}

	public ArrayList<MyFile> getListfiles() {
		return listfiles;
	}

	public void setListfiles(ArrayList<MyFile> listfiles) {
		this.listfiles = listfiles;
	}

	@JSON(serialize = false)
	// serialize参数为false，不会被序列化
	public String getFolder() {
		System.out.println("folder getter……" + folder);
		return folder == null ? "" : folder;
	}

	@JSON(serialize = false)
	// serialize参数为false，不会被序列化
	public String getParent() {
		System.out.println("parent getter……" + parent);
		return parent == null ? "" : parent;
	}

	public void setFolder(String folder) {
		System.out.println("folder setter……");
		this.folder = folder == null ? "" : folder;
	}

	public void setParent(String parent) {
		System.err.println("parrent setter……" + parent);
		this.parent = parent == null ? "" : parent;
	}

	@Override
	public String execute() throws Exception {
		listFileService = new ListFileService();
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String username = user.getUsername();
		// 当前要请求的文件夹完整路径
		String currentFolder = Tools.getAcessPath(username, parent, folder);
		listFileService.setCurrentFile(currentFolder);
		System.out.println("当前文件夹：" + currentFolder);

		ArrayList<MyFile> listfiles = listFileService
				.getChildFilesList(username);
		
		ActionContext.getContext().put("currentName",
				listFileService.getCurrentFile());
		ActionContext.getContext().put("fileList", listfiles);
		System.out.println("执行execute方法");
		return SUCCESS;
	}

	public String listAll() throws ParseException {
		listFileService = new ListFileService();
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String userroot = user.getUsername();
		// 当前要请求的文件夹完整路径
		String currentFolder = Tools.getAcessPath(userroot, getParent(),
				getFolder());
		System.out.println("currentFolder:" + currentFolder);
		listFileService.setCurrentFile(currentFolder);
		listfiles = listFileService.getChildFilesList(userroot);
//		listfiles=listFileService.listUserFiles(user.getUsername());
		totalFileNum = listfiles.size();
		ActionContext.getContext().getSession().put("listfiles", listfiles);
		System.out.println("执行getList方法");
		return SUCCESS;
	}
	
	public String listUserRecycal() throws ParseException {
		listFileService = new ListFileService();
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String userroot = user.getUsername();
		// 当前要请求的文件夹完整路径
		String currentFolder = Tools.getRecycalPath(userroot);
		System.out.println("re:" + currentFolder);
		listFileService.setCurrentFile(currentFolder);
		listfiles = listFileService.getChildFilesList(userroot);
		totalFileNum = listfiles.size();
		System.out.println("执行getList方法");
		return SUCCESS;
	}
	
	public String listDisk() throws ParseException {
		System.out.println("请求地址："+folder);
		listFileService=new ListFileService();
		String path=new java.io.File(parent, folder).getAbsolutePath();
		listfiles=listFileService.listDisk(path);
		int fileSize=listfiles.size();
		System.out.println("filesize="+fileSize);
		totalFileNum = fileSize;
		return SUCCESS;
	}

}
